from langchain_core.documents import Document


def to_document_bpr(file_dict, file):
    docus = []

    for para in file_dict["paragraphInfos"]:
        if para["text"] != "":
            docu = Document(page_content=para["text"], metadata={"fileId": file.get("docId"),
                                                                 "filePath": file_dict["filePath"],
                                                                 "fileName": file.get("documentName"),
                                                                 "index": para["index"],
                                                                 "sectionIndex": para["sectionIndex"],
                                                                 "bodyChildIndex": para["bodyChildIndex"],
                                                                 "text": para["text"],
                                                                 "tableCellInfos": None
                                                                 }
                            )
            docus.append(docu)

    for table in file_dict["tableInfos"]:
        for cell in table["tableCellInfos"]:
            cell_dict = {
                "fileId": file.get("docId"),
                "filePath": file_dict["filePath"],
                "fileName": file.get("documentName"),
                "index": table["index"],
                "sectionIndex": table["sectionIndex"],
                "bodyChildIndex": table["bodyChildIndex"],
                "text": None,
                "tableCellInfos": [{
                    "rowIndex": cell["rowIndex"],
                    "columnIndex": cell["columnIndex"],
                    "paragraphInfos": []
                }]
            }
            cell_text = ""
            for para in cell["paragraphInfos"]:
                para_dict = {
                    "index": para["index"],
                    "sectionIndex": para["sectionIndex"],
                    "bodyChildIndex": para["bodyChildIndex"],
                    "text": para["text"]
                }
                cell_dict["tableCellInfos"][0]["paragraphInfos"].append(para_dict)
                cell_text = cell_text + para["text"] + "\n"
            docu = Document(page_content=cell_text.rstrip("\n"), metadata=cell_dict)
            docus.append(docu)

    return docus


def to_document_sop(file_dict, file):
    docus = []

    for para in file_dict["paragraphInfos"]:
        if para["text"] != "":
            docu = Document(page_content=para["text"], metadata={"fileId": file.get("docId"),
                                                                 "filePath": file_dict["filePath"],
                                                                 "fileName": file.get("documentName"),
                                                                 "index": para["index"],
                                                                 "sectionIndex": para["sectionIndex"],
                                                                 "bodyChildIndex": para["bodyChildIndex"],
                                                                 "text": para["text"],
                                                                 "tableCellInfos": None
                                                                 }
                            )
            docus.append(docu)

    for table in file_dict["tableInfos"]:
        table_dict = {
            "fileId": file.get("docId"),
            "filePath": file_dict["filePath"],
            "fileName": file.get("documentName"),
            "index": table["index"],
            "sectionIndex": table["sectionIndex"],
            "bodyChildIndex": table["bodyChildIndex"],
            "text": None,
            "tableCellInfos": []
        }
        table_text = ""
        for cell in table["tableCellInfos"]:
            cell_dict = {
                "rowIndex": cell["rowIndex"],
                "columnIndex": cell["columnIndex"],
                "paragraphInfos": []
            }
            cell_text = ""
            for para in cell["paragraphInfos"]:
                para_dict = {
                    "index": para["index"],
                    "sectionIndex": para["sectionIndex"],
                    "bodyChildIndex": para["bodyChildIndex"],
                    "text": para["text"]
                }
                cell_dict["paragraphInfos"].append(para_dict)
                cell_text += para["text"]
            table_dict["tableCellInfos"].append(cell_dict)
            table_text += cell_text + "|"
        docu = Document(page_content=table_text, metadata=table_dict)
        docus.append(docu)
    return docus


